from packages.ply import lex

class lexer(object):
    def __init__(self):

        self.keyword = (
                "INPUT"       ,
                "OUTPUT"      ,
                "INOUT"       ,
                "WIRE"        ,
                "REG"         ,
                "LOGIC"       ,
                "MODULE"      ,
                "FUNCTION"    ,
                "ENDFUNCTION" ,
                "TASK"        ,
                "ENDTASK"     ,
                "GENERATE"    ,
                "ENDGENERATE" ,
                "PARAMETER"   ,
                "LOCALPARAM"  ,
                "SIGNED"      ,
                "ASSIGN"      ,
                "UNSIGNED"    ,
                "ENDMODULE"   ,
                "IF"          ,
                "ELSE"        ,
                "BEGIN"       ,
                "END"         ,
                "CASE"        ,
                "ENDCASE"
                )

        self.reversed = {}
        for kw in self.keyword:
            self.reversed[kw.lower()] = kw
        self.tokens = self.keyword + (
                "ID"      ,
                "INF"     ,
                "IFDEF"   ,
                "INCLUDE" ,
                "ENDIF"   ,
                "COMMENT" ,
                "COMMA"   ,
                "LPAREN"  ,
                'RPAREN'  ,
                "COLON"   ,
                "LBRAC"   ,
                "RBRAC"   ,
                "NUMBER"  ,
                "POW"     ,
                "SEM"     ,
                "POINT"   ,
                "GRANTEQ" ,
                "LESSEQ"  ,
                "DELAY"   ,
                "GRANT"   ,
                "LESS"
                )

        self.t_ignore  = " \t\n"
        self.t_COMMA   = r","
        self.t_LPAREN  = r"\("
        self.t_RPAREN  = r"\)"
        self.t_COLON   = r":"
        self.t_LBRAC   = r"\["
        self.t_RBRAC   = r"\]"
        self.t_POW     = r"\*\*"
        self.t_SEM     = r";"
        self.t_GRANTEQ = r">="
        self.t_LESSEQ  = r"<="
        self.t_GRANTEQ = r">"
        self.t_LESSEQ  = r"<"
        self.t_INCLUDE = r"`include"

        self.literals = '+-*/?=`#"$'
    def build(self,**kwargs):
        self.lexer = lex.lex(object=self,**kwargs)

    def token(self):
        return self.lexer.token()

    def input(self,data):
        self.lexer.input(data)
    
    def skip(self,n):
        self.lexer.skip(n)

    def t_error(self,t):
        self.lexer.skip(1)

    def t_INF(self,t):
        r"\b[_a-zA-Z]\w*\b\.\b[_a-zA-Z]\w*\b"
        return t

    def t_ID(self,t):
        r"\b[_a-zA-Z]\w*\b"
        t.type = self.reversed.get(t.value,"ID")
        return t

    def t_NUMBER(self,t):
        r"\d+"
        return t

    def t_POINT(self,t):
        r"\."
        return t

    def t_COMMENT(self,t):
        r"//.*"
        pass
    def t_IFDEF(self,t):
        r"`ifdef"
        return t
    def t_ENDIF(self,t):
        r"`endif"
        return t

    def t_DELAY(self,t):
        r"\#\w+"
        pass



if __name__ == "__main__":
    t = lexer()
    t.build()
    t.input("input a,b,c")
    print(t.token())
    t.skipIgnore()
    t.skipIgnore()
    print(t.peek())
    print(t.peek())
    print(t.peek())
    print(t.token())
    print(t.token())
